﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Drawing;

public partial class CustomerOrderInfo : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;

        if (!IsPostBack)
        {
            LoadCustData();
        }
    }

    protected void btnSubmit_Click(object sender, System.EventArgs e)
    {

        SqlConnection conn = new SqlConnection("user id=portaluser;data source=" + Session["Servername"] + ";initial catalog=" + Session["ProjectName"] + ";password=sqluser");

        string sqlcommand = "update " + Session["campaignname"] + "_CallData set firstname = @firstname" +
                    ", lastname = @lastname" +
                    ", primaryphone = @primaryphone" +
                    ", alternatephone = @alternatephone" +
                    ", email = @email" +
                    " where enterpriselistid = @listid";

        SqlCommand cmd = new SqlCommand(sqlcommand, conn);

        cmd.Parameters.Add(new SqlParameter("@firstname", SqlDbType.VarChar));
        cmd.Parameters.Add(new SqlParameter("@lastname", SqlDbType.VarChar));
        cmd.Parameters.Add(new SqlParameter("@primaryphone", SqlDbType.VarChar));
        cmd.Parameters.Add(new SqlParameter("@alternatephone", SqlDbType.VarChar));
        cmd.Parameters.Add(new SqlParameter("@email", SqlDbType.VarChar));
        cmd.Parameters.Add(new SqlParameter("@listid", SqlDbType.Int));
        cmd.Parameters["@firstname"].Value = tbFirstName.Text;
        cmd.Parameters["@lastname"].Value = tbLastName.Text;
        cmd.Parameters["@primaryphone"].Value = tbPriPhone.Text;
        cmd.Parameters["@alternatephone"].Value = tbSecPhone.Text;
        cmd.Parameters["@email"].Value = tbEmail.Text;
        cmd.Parameters["@listid"].Value = Session["listid"];

        if (IsTelephone(tbPriPhone.Text))
        {

            try
            {
                cmd.Connection.Open();

                if (cmd.ExecuteNonQuery() == 1)
                {
                    lblError.ForeColor = Color.Blue;
                    lblError.Text = "Contact Info submitted successfully";
                }
                else
                {
                    lblError.ForeColor = Color.Red;
                    lblError.Text = "Failed to update Contact Info";
                }

                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                lblError.ForeColor = Color.Red;
                lblError.Text = "Exception: " + ex;
            }
        }
        else
        {
            lblError.ForeColor = Color.Red;
            lblError.Text = "Invalid Phone";
        }

    }

    private bool IsTelephone(string strTel)
    {
        if (strTel.Trim().Length == 10)
        {
            long result;

            return Int64.TryParse(strTel, out result);
        }
        else
        {
            return false;
        }
    }

    public void LoadCustData()
    {
        String connString = "user id=portaluser;data source=" + Session["Servername"] + ";initial catalog=" + Session["ProjectName"] + ";password=sqluser";
        SqlConnection conn = new SqlConnection(connString);
        conn.ConnectionString = connString;
        conn.Open();
        System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();
        string lccommand = null;

        lccommand = "Select * from " + Session["campaignname"] + "_CallData with (NOLOCK) where Enterpriselistid = " + Session["listid"];
        command.CommandType = CommandType.Text;
        command.CommandText = lccommand;
        command.Connection = conn;
        SqlDataReader dr = default(SqlDataReader);
        dr = command.ExecuteReader(CommandBehavior.SingleRow);
        if (dr.HasRows)
        {
            dr.Read();

            tbListid.Text = dr["enterpriselistid"].ToString();
            tbFirstName.Text = dr["firstname"].ToString();
            tbLastName.Text = dr["lastname"].ToString();
            tbPriPhone.Text = dr["primaryphone"].ToString();
            tbSecPhone.Text = dr["alternatephone"].ToString();
            tbEmail.Text = dr["email"].ToString();

            dr.Close();
            dr = null;


        }

        conn.Close();

    }
    //toggles additional shipping information and fills shipping info with previously given info
    protected void cbShip_CheckedChanged(object sender, EventArgs e)
    {
        if (cbShip.Checked)
        {
            pnlShip.Visible = true;
            tbshipFirstName.Text = tbFirstName.Text;
            tbshipLastName.Text = tbLastName.Text;
            tbshipAddr.Text = tbAddr.Text;
            tbshipAddr2.Text = tbAddr2.Text;
            tbshipCity.Text = tbCity.Text;
            tbshipState.Text = tbState.Text;
            tbshipZip.Text = tbZip.Text;
            tbshipCountry.Text = tbCountry.Text;
        }
        else
        {
            pnlShip.Visible = false;
        }
    }
}